home *** CD-ROM | disk | FTP | other *** search
- //***********************************************************************
- //
- // Paint2.cpp
- //
- //***********************************************************************
-
- #define OEMRESOURCE
-
- #include <afxwin.h>
- #include "Resource.h"
- #include "Paint2.h"
-
- CMyApp myApp;
-
- /////////////////////////////////////////////////////////////////////////
- // CMyApp member functions
-
- BOOL CMyApp::InitInstance ()
- {
- m_pMainWnd = new CMainWindow;
- m_pMainWnd->ShowWindow (m_nCmdShow);
- m_pMainWnd->UpdateWindow ();
- return TRUE;
- }
-
- /////////////////////////////////////////////////////////////////////////
- // CMainWindow message map and member functions
-
- BEGIN_MESSAGE_MAP (CMainWindow, CFrameWnd)
- ON_WM_PAINT ()
- ON_COMMAND (IDM_FILE_NEW, OnFileNew)
- ON_COMMAND (IDM_FILE_EXIT, OnFileExit)
- ON_COMMAND_RANGE (IDM_WIDTH_VTHIN, IDM_WIDTH_VTHICK, OnWidth)
- ON_COMMAND_RANGE (IDM_COLOR_BLACK, IDM_COLOR_WHITE, OnColor)
- ON_UPDATE_COMMAND_UI (IDM_FILE_NEW, OnUpdateFileNewUI)
- ON_UPDATE_COMMAND_UI_RANGE (IDM_WIDTH_VTHIN, IDM_WIDTH_VTHICK,
- OnUpdateWidthUI)
- ON_UPDATE_COMMAND_UI_RANGE (IDM_COLOR_BLACK, IDM_COLOR_WHITE,
- OnUpdateColorUI)
- ON_WM_LBUTTONDOWN ()
- ON_WM_MOUSEMOVE ()
- ON_WM_LBUTTONUP ()
- ON_WM_CONTEXTMENU ()
- ON_WM_MEASUREITEM ()
- ON_WM_DRAWITEM ()
- END_MESSAGE_MAP ()
-
- const COLORREF CMainWindow::crColors[8] = {
- RGB ( 0, 0, 0), // Black
- RGB ( 0, 0, 255), // Blue
- RGB ( 0, 255, 0), // Green
- RGB ( 0, 255, 255), // Cyan
- RGB (255, 0, 0), // Red
- RGB (255, 0, 255), // Magenta
- RGB (255, 255, 0), // Yellow
- RGB (255, 255, 255) // White
- };
-
- CMainWindow::CMainWindow ()
- {
- m_nColor = IDM_COLOR_RED;
- m_nWidth = IDM_WIDTH_MEDIUM;
- m_lineArray.SetSize (0, 64);
-
- CString strWndClass = AfxRegisterWndClass (
- 0,
- myApp.LoadStandardCursor (IDC_CROSS),
- (HBRUSH) (COLOR_WINDOW + 1),
- myApp.LoadIcon (IDR_MAINFRAME)
- );
-
- Create (strWndClass, "Paint2", WS_OVERLAPPEDWINDOW,
- rectDefault, NULL, MAKEINTRESOURCE (IDR_MAINFRAME));
-
- LoadAccelTable (MAKEINTRESOURCE (IDR_MAINFRAME));
-
- CMenu* pMenu = GetMenu ();
- for (int i=0; i<8; i++)
- pMenu->ModifyMenu (IDM_COLOR_BLACK + i, MF_BYCOMMAND |
- MF_OWNERDRAW, IDM_COLOR_BLACK + i);
- }
-
- CMainWindow::~CMainWindow ()
- {
- DeleteAllLines ();
- }
-
- void CMainWindow::OnPaint ()
- {
- CPaintDC dc (this);
- int nCount = m_lineArray.GetSize ();
-
- if (nCount) {
- for (int i=0; i<nCount; i++)
- ((CLine*) m_lineArray[i])->Draw (&dc);
- }
- }
-
- void CMainWindow::OnFileNew ()
- {
- DeleteAllLines ();
- Invalidate ();
- }
-
- void CMainWindow::OnUpdateFileNewUI (CCmdUI* pCmdUI)
- {
- pCmdUI->Enable (m_lineArray.GetSize ());
- }
-
- void CMainWindow::OnFileExit ()
- {
- SendMessage (WM_CLOSE, 0, 0);
- }
-
- void CMainWindow::OnWidth (UINT nID)
- {
- m_nWidth = nID;
- }
-
- void CMainWindow::OnColor (UINT nID)
- {
- m_nColor = nID;
- }
-
- void CMainWindow::OnUpdateWidthUI (CCmdUI* pCmdUI)
- {
- pCmdUI->SetCheck (pCmdUI->m_nID == m_nWidth);
- }
-
- void CMainWindow::OnUpdateColorUI (CCmdUI* pCmdUI)
- {
- pCmdUI->SetCheck (pCmdUI->m_nID == m_nColor);
- }
-
- void CMainWindow::OnLButtonDown (UINT nFlags, CPoint point)
- {
- m_ptFrom = point;
- m_ptTo = point;
- SetCapture ();
- }
-
- void CMainWindow::OnMouseMove (UINT nFlags, CPoint point)
- {
- if (GetCapture () == this) {
- CClientDC dc (this);
- InvertLine (&dc, m_ptFrom, m_ptTo);
- InvertLine (&dc, m_ptFrom, point);
- m_ptTo = point;
- }
- }
-
- void CMainWindow::OnLButtonUp (UINT nFlags, CPoint point)
- {
- static UINT nWidths[5] = { 1, 8, 16, 24, 32 };
-
- if (GetCapture () == this) {
- ReleaseCapture ();
-
- CClientDC dc (this);
- InvertLine (&dc, m_ptFrom, m_ptTo);
- CLine* pLine = NULL;
-
- try {
- pLine = new CLine (m_ptFrom, m_ptTo,
- nWidths[m_nWidth - IDM_WIDTH_VTHIN],
- crColors[m_nColor - IDM_COLOR_BLACK]);
-
- m_lineArray.Add (pLine);
- pLine->Draw (&dc);
- }
- catch (CMemoryException* e) {
- MessageBox ("Out of memory. You must clear the " \
- "drawing area before adding more lines.", "Error",
- MB_ICONEXCLAMATION | MB_OK);
-
- if (pLine != NULL)
- delete pLine;
- e->Delete ();
- }
- }
- }
-
- void CMainWindow::OnContextMenu (CWnd* pWnd, CPoint point)
- {
- CRect rect;
- GetClientRect (&rect);
- ClientToScreen (&rect);
-
- if (rect.PtInRect (point)) {
- CMenu menu;
- menu.LoadMenu (IDR_CONTEXTMENU);
- CMenu* pContextMenu = menu.GetSubMenu (0);
-
- for (int i=0; i<8; i++)
- pContextMenu->ModifyMenu (IDM_COLOR_BLACK + i,
- MF_BYCOMMAND | MF_OWNERDRAW, IDM_COLOR_BLACK + i);
-
- pContextMenu->TrackPopupMenu (TPM_LEFTALIGN | TPM_LEFTBUTTON |
- TPM_RIGHTBUTTON, point.x, point.y, this);
- return;
- }
- CFrameWnd::OnContextMenu (pWnd, point);
- }
-
- void CMainWindow::OnMeasureItem (int nIDCtl, LPMEASUREITEMSTRUCT lpmis)
- {
- lpmis->itemWidth = ::GetSystemMetrics (SM_CYMENU) * 4;
- lpmis->itemHeight = ::GetSystemMetrics (SM_CYMENU);
- }
-
- void CMainWindow::OnDrawItem (int nIDCtl, LPDRAWITEMSTRUCT lpdis)
- {
- BITMAP bm;
- CBitmap bitmap;
- bitmap.LoadOEMBitmap (OBM_CHECK);
- bitmap.GetObject (sizeof (bm), &bm);
-
- CDC dc;
- dc.Attach (lpdis->hDC);
-
- CBrush* pBrush = new CBrush (::GetSysColor ((lpdis->itemState &
- ODS_SELECTED) ? COLOR_HIGHLIGHT : COLOR_MENU));
- dc.FrameRect (&(lpdis->rcItem), pBrush);
- delete pBrush;
-
- if (lpdis->itemState & ODS_CHECKED) {
- CDC dcMem;
- dcMem.CreateCompatibleDC (&dc);
- CBitmap* pOldBitmap = dcMem.SelectObject (&bitmap);
-
- dc.BitBlt (lpdis->rcItem.left + 4, lpdis->rcItem.top +
- (((lpdis->rcItem.bottom - lpdis->rcItem.top) -
- bm.bmHeight) / 2), bm.bmWidth, bm.bmHeight, &dcMem,
- 0, 0, SRCCOPY);
-
- dcMem.SelectObject (pOldBitmap);
- }
-
- pBrush = new CBrush (crColors[lpdis->itemID - IDM_COLOR_BLACK]);
- CRect rect = lpdis->rcItem;
- rect.DeflateRect (6, 4);
- rect.left += bm.bmWidth;
- dc.FillRect (rect, pBrush);
- delete pBrush;
-
- dc.Detach ();
- }
-
- void CMainWindow::InvertLine (CDC* pDC, CPoint ptFrom, CPoint ptTo)
- {
- int nOldMode = pDC->SetROP2 (R2_NOT);
-
- pDC->MoveTo (ptFrom);
- pDC->LineTo (ptTo);
-
- pDC->SetROP2 (nOldMode);
- }
-
- void CMainWindow::DeleteAllLines ()
- {
- int nCount = m_lineArray.GetSize ();
-
- for (int i=0; i<nCount; i++)
- delete m_lineArray[i];
-
- m_lineArray.RemoveAll ();
- }
-
- /////////////////////////////////////////////////////////////////////////
- // CLine member functions
-
- CLine::CLine (CPoint ptFrom, CPoint ptTo, UINT nWidth, COLORREF crColor)
- {
- m_ptFrom = ptFrom;
- m_ptTo = ptTo;
- m_nWidth = nWidth;
- m_crColor = crColor;
- }
-
- void CLine::Draw (CDC* pDC)
- {
- CPen pen (PS_SOLID, m_nWidth, m_crColor);
-
- CPen* pOldPen = pDC->SelectObject (&pen);
- pDC->MoveTo (m_ptFrom);
- pDC->LineTo (m_ptTo);
-
- pDC->SelectObject (pOldPen);
- }
-